import java.util.Arrays;
import java.util.OptionalInt;
import java.util.Vector;

/**
 * @author : Jame
 * @date : 2024/6/28 下午 1:47
 */
public class SleepSort {

    public static void main(String[] args) {
        int[] data = {100, 200, 500, 300, 600};
        //这里因为是多线程的环境，所以使用线程安全的Vector
        Vector<Integer> result = new Vector<>();
        for (int i = 0; i < data.length; i++) {
            int finalI = i;
            new Thread(() -> {
                try {
                    Thread.sleep(data[finalI]);
                    result.add(data[finalI]);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }).start();
        }
        OptionalInt max = Arrays.stream(data).max();
        if (!max.isPresent()) {
            throw new IllegalArgumentException("需要排序的数据不正确");
        }
        int maxNumber = max.getAsInt();
        try {
            //需要等待最大的数据睡眠时间,才能保证下面打印的是全部的数据
            Thread.sleep(maxNumber);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        for (Integer i : result) {
            System.out.println(i);
        }
    }

}
